﻿
<div class="@Class">
    <div class="row">
        @if (ShowDate)
        {
            <div class="@(ShowTime?"col-12 col-sm-6":"col-12")">
                <MMenu @bind-Value="_menu"
                       CloseOnContentClick="false"
                       Transition="scale-transition"
                       OffsetY
                       MinWidth="@("auto")">
                    <ActivatorContent>
                         <MTextField @bind-Value="_date" HideDetails="true" Disabled="Disabled"
                                    Label="@Label"
                                    PrependIcon="mdi-calendar"
                                    Readonly
                                    @attributes="context.Attrs"/>
                    </ActivatorContent>
                    <ChildContent>
                        <MDatePicker Value="_date" ValueChanged="OnDateChanged" TValue="DateOnly?" 
                                     Locale="zh-CN"
                                     ActivePicker="_activePicker"
                                     OnActivePickerUpdate="val=>_activePicker=val"
                                     Max="DateOnly.FromDateTime(DateTime.Now.AddYears(2))"
                                     OnInput="()=>_menu=false"></MDatePicker>
                    </ChildContent>
                </MMenu>
            </div>
        }
        @if (ShowTime)
        {
            <div class="@(ShowDate?"col-12 col-sm-6":"col-12")">
                <MMenu @bind-Value="_menu2"
                       CloseOnContentClick="false"
                       NudgeRight="40"
                       Transition="scale-transition"
                       OffsetY
                       MaxWidth="290"
                       MinWidth="290">
                    <ActivatorContent>
                         <MTextField @bind-Value="_time" HideDetails="true" Disabled="Disabled"
                                    Label="@Label"
                                    PrependIcon="mdi-clock-time-four-outline"
                                    Readonly
                                    @attributes="context.Attrs"></MTextField>
                    </ActivatorContent>
                    <ChildContent>
                        @if (_menu2)
                        {
                            <MTimePicker Value="_time" ValueChanged="OnTimeChanged" 
                                         FullWidth></MTimePicker>
                        }
                    </ChildContent>
                </MMenu>
            </div>
        }

    </div>

</div>

@code {
    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public DateTime Value { get; set; }
    [Parameter]
    public EventCallback<DateTime> ValueChanged { get; set; }
    [Parameter]
    public DateTime? NullableValue { get; set; }
    [Parameter]
    public EventCallback<DateTime?> NullableValueChanged { get; set; }
    [Parameter]
    public bool ShowTime { get; set; } = true;
    [Parameter]
    public bool ShowDate { get; set; } = true;
    [Parameter]
    public string Label { get; set; }
    [Parameter]
    public bool Nullable { get; set; }
    [Parameter]
    public bool Disabled { get; set; }

    private DatePickerType _activePicker = DatePickerType.Year;
    private DateOnly? _date;
    private bool _menu;

    private TimeOnly? _time;
    private bool _menu2;
    private bool _modal2;

    protected override void OnParametersSet()
    {
        if(Nullable)
        {
            if(NullableValue != null)
            {

                _date = DateOnly.FromDateTime(NullableValue.Value);
                _time = TimeOnly.FromDateTime(NullableValue.Value);
            }
            else
            {
                _time = null;
                _date = null;
            }
        }
        else
        {
            _date = DateOnly.FromDateTime(Value);
            _time = TimeOnly.FromDateTime(Value);
        }

    }

    public async Task OnDateChanged(DateOnly? date)
    {
        _date = date;
        if (ShowTime && _time == null)
        {
            return;
        }

        if(date == null)
        {
            if(NullableValue != null)
            {
                NullableValue = null;
                await NullableValueChanged.InvokeAsync(NullableValue);
            }

            return;
        }

        NullableValue = Value = date.Value.ToDateTime(ShowTime ? _time.Value : TimeOnly.MinValue);
        await ValueChanged.InvokeAsync(Value);
        await NullableValueChanged.InvokeAsync(NullableValue);
    }

    public async Task OnTimeChanged(TimeOnly? time)
    {
        _time = time;
        if (ShowDate && _date == null)
        {
            return;
        }

        if (time == null)
        {
            if (NullableValue != null)
            {
                NullableValue = null;
                await NullableValueChanged.InvokeAsync(NullableValue);
            }

            return;
        }

        NullableValue = Value = (ShowDate ? _date : DateOnly.MinValue).Value.ToDateTime(_time.Value);
        await ValueChanged.InvokeAsync(Value);
        await NullableValueChanged.InvokeAsync(NullableValue);
    }
}
